/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/<a href="https://www.kaaction.com">KAA</a>"><a href="https://www.kaaction.com">KAA</a></a> All rights reserved.
 */
package com.kaaction.modules.ka.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.kaaction.common.config.Global;
import com.kaaction.common.persistence.Page;
import com.kaaction.common.web.BaseController;
import com.kaaction.common.utils.StringUtils;
import com.kaaction.modules.ka.entity.BasicGoods;
import com.kaaction.modules.ka.service.BasicGoodsService;

/**
 * 货品基础信息Controller
 * @author huanghaiming
 * @version 2017-04-24
 */
@Controller
@RequestMapping(value = "${adminPath}/ka/basicGoods")
public class BasicGoodsController extends BaseController {

	@Autowired
	private BasicGoodsService basicGoodsService;
	
	@ModelAttribute
	public BasicGoods get(@RequestParam(required=false) String id,String supplierId) {
		BasicGoods entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = basicGoodsService.get(id);
			
			String sizeId = entity.getSizeId();//尺码
			String colorId = entity.getColorId();//颜色
			String canalId = entity.getCanalId();//渠道
			if(!StringUtils.isBlank(sizeId)){
				entity.setSids(sizeId.split(","));
			}
			if(!StringUtils.isBlank(colorId)){
				entity.setCids(colorId.split(","));
			}
			if(!StringUtils.isBlank(canalId)){
				entity.setCaids(canalId.split(","));
			}
		}
		if (entity == null){
			entity = new BasicGoods();
			if(!StringUtils.isBlank(supplierId)){
				entity.setSupplierId(supplierId);
			}
			
		}
		return entity;
	}
	
	@RequiresPermissions("ka:basicGoods:view")
	@RequestMapping(value = {"list", ""})
	public String list(BasicGoods basicGoods, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<BasicGoods> page = basicGoodsService.findPage(new Page<BasicGoods>(request, response), basicGoods); 
		model.addAttribute("page", page);
		return "modules/ka/basicGoodsList";
	}

	@RequiresPermissions("ka:basicGoods:view")
	@RequestMapping(value = "form")
	public String form(BasicGoods basicGoods, Model model) {
		model.addAttribute("basicGoods", basicGoods);
		return "modules/ka/basicGoodsForm";
	}

	@RequiresPermissions("ka:basicGoods:edit")
	@RequestMapping(value = "save")
	public String save(BasicGoods basicGoods, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, basicGoods)){
			return form(basicGoods, model);
		}
		
		int cnt = basicGoodsService.getEqual(basicGoods);
		if(cnt>0){
			model.addAttribute("message", "该供应商的商品已存在");
			return form(basicGoods, model);
		}
				
		basicGoodsService.save(basicGoods);
		addMessage(redirectAttributes, "保存货品基础信息成功");
		return "redirect:"+Global.getAdminPath()+"/ka/basicGoods/?repage";
	}
	
	@RequiresPermissions("ka:basicGoods:delete")
	@RequestMapping(value = "delete")
	public String delete(BasicGoods basicGoods, RedirectAttributes redirectAttributes) {
		basicGoodsService.delete(basicGoods);
		addMessage(redirectAttributes, "删除货品基础信息成功");
		return "redirect:"+Global.getAdminPath()+"/ka/basicGoods/?repage";
	}
	
	@RequiresPermissions("ka:basicGoods:delete")
	@RequestMapping(value = "deleteAll")
	public String deleteAll(BasicGoods basicGoods, RedirectAttributes redirectAttributes) {
		basicGoodsService.deleteAll(basicGoods);
		addMessage(redirectAttributes, "删除货品基础信息成功");
		return "redirect:"+Global.getAdminPath()+"/ka/basicGoods/?repage";
	}
	
}